<?php
require ('easyrest/Server.php');

//Your function or class.
/* ---------------------------------------------------------------------------*/ 

Class My_class 
{
    function method($int, $string="", $boolean="", $string="", $empty="")
    {       
       $data['cities'][] = array('city_id' => '1', 'name'=> 'istanbul', 'option'=> $boolean); 
       $data['cities'][] = array('city_id' => '2', 'name'=> 'nyc', 'some utf-8 chars: çşğü'); 
       $data['cities'][] = array('city_id' => '3', 'name'=> 'london');
       $data['cities'][] = array('city_id' => '4', 'name'=> 'paris', 'string'=> $string);
       
       $data['post'][]   = array(
                                        'post_var1' => $_POST['post_var1'],  //==> WARNING! user your input filter
                                        'post_var2' => $_POST['post_var2'],
                                        );
        
       return $data; 
    }
    
    function method2($int='',$string='',$array='')
    {
       $data['countries'][] = array('country_id' => '1', 'name'=> 'usa');  
       $data['countries'][] = array('country_id' => '2', 'name'=> 'turkey');
       $data['countries'][] = array('country_id' => '3', 'name'=> 'africa');
       
       return $data;  
    }
    
    function method3()
    {
       $data['names'][] = array('id' =>  1, 'name'=> 'john');  
       $data['names'][] = array('id' =>  2, 'name'=> 'peter');  
       
       return $data;  
    }

}

function my_function()
{
       $data['fruits'][] = array('name' => 'banana', 'no'=> 1);
       $data['fruits'][] = array('name' => 'apple',  'no'=> 2);
       $data['fruits'][] = array('name' => 'orange', 'no'=> 3);
       
       return $data;
}

//Config part.
/* ---------------------------------------------------------------------------*/ 

$server = new Server();
//$server->config->service('rest'); // since version 1.0.1 rpc and rest switch.
$server->config->apikey_function('callback_check_apikey');

/* ---------------------------------------------------------------------------*/


//Callback check apikey part.
/* ---------------------------------------------------------------------------*/   

function callback_check_apikey()       
{
    //multiple keys - it should be come from db -> Select from apikeys where id = $id
    $db_apikeys = array(                              
            //client_id            //secretkey              //create an apikey
                     '1' => array('my secret keyword here','my api key'),  //secretkey => apikey
                     '2' => array('my secret keyword here2','my api key2'),
                     '3' => array('my secret keyword here3','my api key3')
                    );
                    
    
    //incoming keys                
    $apikey = Server::get_apikey();  //client apikey 
    $id     = Server::get_id();      //client_id
    
    
    //compare client and server keys
    if(!isset($db_apikeys[$id]))
    return FALSE;
    
    $key = $db_apikeys[$id];
    $encrypted_key = Server::encrypt($key[0],$key[1]);
    
    if($apikey == $encrypted_key)
    return TRUE;
    
    //else false 
    return FALSE; 
}                                                


//Define your functions.
/* ---------------------------------------------------------------------------*/ 

$server->define(

    array(
            'example.test1' => array(
                'function'  => 'My_class::method', //class::method
                'params'    => array('0'  => 'int|min[1]|max[5]',
                                     '1'  => 'string|maxchar[30]', //maximum character length
                                     '2'  => 'boolean',                         
                                     '3'  => 'string|maxchar[30]|filter[false]', //easyrest input_filter switch on/off
                                                                                 //this will allow html but incase                                                                                          //you MUST use your own filter.
                                     '4'  => 'boolean'                           //you must use your xss filter
                                                                                 //default value true.
                                     )
                //'request' => array()
                ),
                
            'example.test2' => array(               
                'function'  => 'My_class::method2', //class::method
                'params'    => array(
                                     '0'  => 'int',
                                     '1'  => 'string', //default filter true.
                                     '2'  => 'array'
                                     )
                ),
                
            
                
            'example.test3' => array(               
                'function'  => 'my_function', //method (func)
                'params'    => array(),  // no params
                ),
                
                
            'example.test4' => array(               
                'function'  => 'My_class::method3', //class::method
                'params'    => array()  // no params..
            ),
                       
         )

);


//Start serving...
/* ---------------------------------------------------------------------------*/ 

$server->serve();

/* ---------------------------------------------------------------------------*/    




?>
